﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using CIAWorldMap;
using MapSurface;

namespace LaoMap
{
    class CIARender
    {
        readonly Dictionary<string, object> _wanted = new Dictionary<string, object>();

        public delegateReport _delegateReport;
        
        readonly Surface _surface;

        public CIARender(Surface surface)
        {
            _surface = surface;
        }

        public void DoPlotPoly(Dictionary<string, MapSegment> _segments2, LaosBorder border, Canvas canvas)
        {
            foreach (string skey1 in _wanted.Keys)
            {
                string[] sz = skey1.Split(' ');
                Debug.Write(" ," + sz[1]);
            }
            Debug.WriteLine("");

            DoPlotPolyLineLaos(border, canvas);
            DoPlotPoly1(_segments2, false, canvas);
        }

        private void DoPlotPolyLineLaos(LaosBorder border, Canvas canvas)
        {
            if (border._geoPolygon.Count == 0)
                return;

            var b = Brushes.DarkGreen;

            Polyline polly = new Polyline();

            polly.StrokeThickness = 5;
            polly.Stroke = b;

            polly.Points = new PointCollection();
            polly.StrokeStartLineCap = PenLineCap.Flat;
            polly.StrokeEndLineCap = PenLineCap.Flat;
            polly.StrokeLineJoin = PenLineJoin.Round;

            foreach (Coord c in border._geoPolygon)
            {
                double px = _surface.GetPx(c._lon);
                double py = _surface.GetPy(c._lat);

                polly.Points.Add(new Point(px, py));
            }

            canvas.Children.Add(polly);
        }

        private void DoPlotPoly1(Dictionary<string, MapSegment> segments, bool showLaos, Canvas canvas)
        {
            int decimate = 0;
            foreach (string skey in segments.Keys)
            {
                if (skey.Contains("rank 1 ") && !showLaos)
                {
                    continue;
                }
                var b = Brushes.Black;
                if (skey.Contains("rank 4 "))
                {
                    b = Brushes.Red; // admin boundary  River????
                }

                if (skey.Contains("rank 1 "))
                {
                    b = Brushes.DarkGreen; // Country border

                    if (_wanted.ContainsKey(skey))
                        b = Brushes.DarkRed; // Country border
                }
                if (skey.Contains("rank 13 "))
                {
                    b = Brushes.Pink; // Country border
                }

                if (skey.Contains("rank 3 "))
                {
                    b = Brushes.DarkOrange; // Country border
                }

                if (skey.Contains("rank 5 "))
                {
                    b = Brushes.Blue; // Country border
                }

                if (skey.Contains("rank 2 "))
                {
                    b = Brushes.GreenYellow; // Country border
                }

                MapSegment s = segments[skey];
                foreach (Coord c in s._coords)
                {
                    double px = _surface.GetPx(c._lon);
                    double py = _surface.GetPy(c._lat);

                    if (py > _surface.Height())
                        continue;

                    if (px > _surface.Width())
                        continue;

                    if (py < 0)
                        continue;

                    if (px < 0)
                        continue;

                    if (!_surface.PointInPolygon(new Point(px, py)))
                    {
                        continue;
                    }

                    decimate++;
                    if ((decimate != 10) && !showLaos)
                        continue;
                    decimate = 0;
                    var el = new Ellipse();

                    el.Height = 4;
                    el.Width = 4;

                    //var mySolidColorBrush = new SolidColorBrush(Color.FromArgb(255, 255, 255, 0));

                    // el.Fill = mySolidColorBrush;
                    el.StrokeThickness = 0;
                    el.Fill = b;

                    el.Tag = skey;

                    //   el.MouseDown += new MouseButtonEventHandler(ElMouseDown);
                    el.MouseEnter += DebugOnEnter;
                    
                    Canvas.SetLeft(el, px);
                    Canvas.SetTop(el, py);
                    canvas.Children.Add(el);
                }
            }
        }

        void DebugOnEnter(object o, MouseEventArgs args)
        {
            if (_delegateReport == null)
                return;

            var p = o as Ellipse;
            if (p == null)
                return;
            var szkey = p.Tag as string;
            if (szkey == null)
                return;
            _delegateReport("CIA " + szkey);
        }

    }
}
